/*
package com.java.chatroom.common.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

*/
/**
 * @author wiqi
 * @version 1.0
 * @date 2020/12/24
 * @return java.lang.Object
 * @programcreateQueue();
 * @description 动态创建队列
 * @param: this is no-parameter method.
 * @return java.lang.Object
 * @program createExchange();
 * @description 动态创建交换机
 * @param: this is no-parameter method.
 * @return java.lang.Object
 * @program reateBinding();
 * @description 动态将交换机及队列绑定
 * @param: this is no-parameter method.
 *//*

public class InitRabbitService implements InitializingBean {
    RabbitAdmin rabbitAdmin;

    public InitRabbitService(RabbitAdmin rabbitAdmin) {
        this.rabbitAdmin = rabbitAdmin;
    }

    */
/**
 * @return java.lang.Object
 * @programcreateQueue();
 * @description 动态创建队列
 * @param: this is no-parameter method.
 *//*

    public Queue createQueue(QueueEnum queueEnum) {
        Queue queue =
            new Queue(queueEnum.getName(), queueEnum.isDurable(), queueEnum.isExclusive(), queueEnum.isAutoDelete(),
                queueEnum.getArguments());
        rabbitAdmin.declareQueue(queue);
        return queue;
    }

    */
/**
 * @return java.lang.Object
 * @program createExchange();
 * @description 动态创建交换机
 * @param: this is no-parameter method.
 *//*

    public Exchange createExchange(ExchangeEnum exchangeEnum) {
        // 遍历交换机枚举
        // 声明交换机
        Exchange exchange = null;
        switch (exchangeEnum.getType()) {
            case direct:
                exchange = new DirectExchange(exchangeEnum.getExchangeName(), exchangeEnum.isDurable(), false);
                break;
            case topic:
                exchange = new TopicExchange(exchangeEnum.getExchangeName(), exchangeEnum.isDurable(), false);
                break;
            case fanout:
                exchange = new FanoutExchange(exchangeEnum.getExchangeName(), exchangeEnum.isDurable(), false);
                break;
            case headers:
                exchange = new HeadersExchange(exchangeEnum.getExchangeName(), exchangeEnum.isDurable(), false);
                break;
            default:
                break;
        }
        // 将交换机注册到spring bean工厂 让spring实现交换机的管理
        if (exchange != null) {
            rabbitAdmin.declareExchange(exchange);
        }
        // 不返回任何对象 该方法只用于在spring初始化时 动态的将bean对象注册到spring bean工厂
        return exchange;
    }

    */
/**
 * @return java.lang.Object
 * @program reateBinding();
 * @description 动态将交换机及队列绑定
 * @param: this is no-parameter method.
 *//*

    public void createBinding() {
        // 遍历队列枚举 将队列绑定到指定交换机
        Arrays.stream(QueueEnum.values()).forEach(queueEnum -> {
                // 从spring bean工厂中获取队列对象（刚才注册的）
                Queue queue = createQueue(queueEnum);
                Exchange exchange = createExchange(queueEnum.getExchangeEnum());
                // 声明绑定关系
                Binding binding = null;
                // 根据不同的交换机模式 获取不同的交换机对象（注意：刚才注册时使用的是父类Exchange，这里获取的时候将类型获取成相应的子类）生成不同的绑定规则
                switch (queueEnum.getExchangeEnum().getType()) {
                    case fanout:

                        binding = BindingBuilder.bind(queue).to((FanoutExchange)exchange);
                        break;
                    case topic:
                        binding = BindingBuilder.bind(queue).to((TopicExchange)exchange).with(queueEnum.getRoutingKey());
                        break;
                    case headers:
                        if (queueEnum.isWhereAll()) {
                            // whereAll表示全部匹配
                            binding =
                                BindingBuilder.bind(queue).to((HeadersExchange)exchange).whereAll(queueEnum.getHeads())
                                    .match();
                        } else {
                            binding =
                                BindingBuilder.bind(queue).to((HeadersExchange)exchange).whereAny(queueEnum.getHeads())
                                    .match();
                        }
                        break;
                    case direct:
                    default:
                        binding = BindingBuilder.bind(queue).to((DirectExchange)exchange).with(queueEnum.getRoutingKey());
                        break;
                }
                // 将绑定关系注册到spring bean工厂 让spring实现绑定关系的管理
                if (binding != null) {
                    rabbitAdmin.declareBinding(binding);
                }
            }
        );
    }

    @Override public void afterPropertiesSet() throws Exception {
        createBinding();
    }
}
*/
